package plus.easydo.server.user.mp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.core.vaild.Insert;
import plus.easydo.core.vaild.Update;
import plus.easydo.server.user.mp.dto.OauthClientDetailsDto;
import plus.easydo.server.user.mp.qo.OauthClientDetailsQo;
import plus.easydo.server.user.mp.service.IOauthClientDetailsService;
import plus.easydo.server.user.mp.vo.OauthClientDetailsVo;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.mybatis.plus.result.MPDataResult;
import plus.easydo.starter.oauth.resources.anotation.CustomizePreAuthorize;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.List;

/**
 * oauth2客户端信息Controller
 *
 * @author gebilaoyu
 * @date 2021-05-01
 */
@Api(tags = "客户端管理")
@RestController
@RequestMapping("/clentDetails")
public class OauthClientDetailsController extends MPBaseController {


    @Resource
    private IOauthClientDetailsService oauthClientDetailsService;

    /**
     * 重置客户端密钥
     */
    @SystemLog(value = "重置客户端密钥",saveLog = true)
    @ApiOperation(value = "重置客户端密钥")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:edit"})
    @PutMapping("/resetSecret")
    public R<Object> resetSecret(@RequestBody @Validated({Update.class}) OauthClientDetailsDto dto) {
        dto.setClientSecret(Oauth2Utils.encryptPassword(dto.getClientSecret()));
        return opResult(oauthClientDetailsService.updateOauthClientDetails(dto));
    }

    /**
     * 分页条件查询oauth2客户端信息列表
     */
    @ApiOperation(value = "分页条件查询oauth2客户端信息列表")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:page"})
    @PostMapping("/page")
    public MPDataResult page(OauthClientDetailsQo qo) {
        IPage<OauthClientDetailsVo> page = oauthClientDetailsService.page(qo);
        return ok(page);
    }

    /**
     * 获取所有oauth2客户端信息列表
     */
    @ApiOperation(value = "获取所有oauth2客户端信息列表")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:list"})
    @GetMapping("/list")
    public List<OauthClientDetailsVo> list(OauthClientDetailsQo qo) {
        return oauthClientDetailsService.list(qo);
    }


    /**
     * 获取oauth2客户端信息详细信息
     */
    @ApiOperation(value = "获取oauth2客户端信息详细信息")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:query"})
    @GetMapping(value = "/{clientId}")
    public R<Object> getInfo(@PathVariable("clientId") String clientId)
    {
        return ok(oauthClientDetailsService.selectOauthClientDetailsById(clientId));
    }

    /**
     * 新增oauth2客户端信息
     */
    @SystemLog(value = "新增oauth2客户端信息",saveLog = true)
    @ApiOperation(value = "新增oauth2客户端信息")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:add"})
    @PostMapping
    public R<Object> add(@RequestBody @Validated(Insert.class) OauthClientDetailsDto dto)
    {
        return opResult(oauthClientDetailsService.insertOauthClientDetails(dto));
    }

    /**
     * 修改oauth2客户端信息
     */
    @SystemLog(value = "修改oauth2客户端信息",saveLog = true)
    @ApiOperation(value = "修改oauth2客户端信息")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:edit"})
    @PutMapping
    public R<Object> edit(@RequestBody @Validated(Update.class) OauthClientDetailsDto dto)
    {
        return opResult(oauthClientDetailsService.updateOauthClientDetails(dto));
    }

    /**
     * 删除oauth2客户端信息
     */
    @SystemLog(value = "删除oauth2客户端信息",saveLog = true)
    @ApiOperation(value = "删除oauth2客户端信息")
    @CustomizePreAuthorize(hasPermission = {"usercenter:clentDetails:remove"})
    @DeleteMapping("/{clientIds}")
    public R<Object> remove(@PathVariable String[] clientIds)
    {
        return opResult(oauthClientDetailsService.deleteOauthClientDetailsByIds(clientIds));
    }
}
